package com.huluobo.high3videoproject.startLive.presenter;

import com.huluobo.base.view.BasePresenter;
import com.huluobo.high3videoproject.bean.StartLiveBeanResult;
import com.huluobo.high3videoproject.bean.StopLiveBeanResult;
import com.huluobo.high3videoproject.startLive.model.StartLiveModel;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class StartLivePresenter extends BasePresenter<StartLiveContract.View> implements StartLiveContract.Presenter {
    private StartLiveModel startLiveModel = new StartLiveModel();

    @Override
    public void startLive(String address, String caption) {
        startLiveModel.startLive(address, caption).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<StartLiveBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(StartLiveBeanResult startLiveBeanResult) {
                        if (isAttach()) {
                            getView().onStartLiveSuccess(startLiveBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onStartLiveFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void stopLive() {
        startLiveModel.stopLive().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<StopLiveBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(StopLiveBeanResult stopLiveBeanResult) {
                        if (isAttach()) {
                            getView().onStopLiveSuccess(stopLiveBeanResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (isAttach()) {
                            getView().onStopLiveFailed(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
